import { defineStore } from 'pinia'
import { useUserStore } from '@/store'
interface T {
    count: number
    name: string
}
const useGlobalStore = defineStore('global', {
    state: () => {
        return {
            order: {},
            data: null,
            canIpop: true,
            current: '' as string|number,
            /** 库存提示字段名数组 */
            stockNames: [] as Array<T>,
            /**今日可购买数量 对象 */
            remain: {},
            /** 视频播放器 全局静音标识 默认不静音 */
            muted:false
        }
    },
    actions: {
        setOrder(data: any) {
            this.order = data
        },
        setData(data: any) {
            this.data = data
        },
        setCaniPop(data: any) {
            this.canIpop = data
        },
        setCurrent(data: any) {
            this.current = data
        },
        /**
         * 根据 num 参数 显示不同的库存字段
         * @param num
         */
        setStockName(num: number| string) {
            if (num <= 0) return '缺货'
            if (num < this.stockNames[1].count) return this.stockNames[2].name.replace(/X/, num.toString())
            if (num < this.stockNames[0].count) return this.stockNames[1].name
            return this.stockNames[0].name
        },
        /** 权限验证 */
        permissionCheck() {
            const userStore = useUserStore()
            let isVisitor = userStore.userInfo.type == 3
            let para = {
                merchantId: userStore.userInfo.merchantId
            }
            return new Promise((resolve, reject) => {
                // 游客 可以直接购买
                if (isVisitor) {
                    resolve('')
                    return
                }
                // 2.需要查询是否实名认证
                let status = userStore.config.authStatus
                if (status === 4) {
                    // 3. 检查合同状态
                    uni.$u.$api.MerchantApi.checkOrderContract(para).then((response: any) => {
                        if (response) {
                            resolve(response)
                        } else {
                            uni.$u.$showModal({
                                title: '温馨提示',
                                content: '购买前需要签订采购合同!',
                                confirmText: '去签订',
                                success: (s: any) => {
                                    if (s.confirm) {
                                        uni.$u.$MRouter.webView.to({type:1})
                                    }
                                }
                            })
                            reject('未签合同')
                        }
                    })
                }else{
                    uni.$u.$showModal({
                        title: '认证提醒',
                        content: '您还没有实名认证,将无法使用平台提供的服务',
                        confirmText: '去认证',
                        success: (res: any) => {
                            if (res.confirm) {
                                uni.$u.$MRouter.certification.to()
                            }
                        }
                    })
                    reject('您还没有实名认证,将无法使用平台提供的服务')
                }
            })
        }
    }
})

export default useGlobalStore
